package com.mazaiting.redeye.module.media

import com.mazaiting.easy.base.bean.Response
import com.mazaiting.easy.base.mvp.IBaseView
import com.mazaiting.easy.utils.rx.RxScheduler
import com.mazaiting.log.L
import com.mazaiting.redeye.api.UserApi
import com.mazaiting.redeye.base.ApplicationComponentImpl
import com.mazaiting.redeye.base.BaseCommonPresenter
import com.mazaiting.redeye.base.CustomConsumer
import com.mazaiting.redeye.base.ICancelPresenter
import com.mazaiting.redeye.bean.Request
import com.trello.rxlifecycle3.kotlin.bindToLifecycle
import dagger.Component
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import javax.inject.Inject

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-07-09
 * @description 媒体联系人
 */
class MediaContract {
    interface View : IBaseView {
        /**
         * 加载成功
         * @param data Tab 列表
         */
        fun mediaListSuccess(data: List<String>)

        /**
         * 加载异常
         */
        fun mediaListError()
    }
    interface Presenter : ICancelPresenter<View> {
        /**
         * 加载媒体列表
         */
        fun loadMediaList()
    }
}

/**
 * 媒体联系人
 */
class MediaPresenter
@Inject constructor(
    private val userApi: UserApi
): BaseCommonPresenter<MediaContract.View>(), MediaContract.Presenter {

    /**
     * 媒体 Disposable
     */
    private var mMediaListDispose: Disposable? = null

    override fun loadMediaList() {
        mMediaListDispose = userApi.mediaList(Request(token))
            .compose(RxScheduler.applySchedulers())
            .bindToLifecycle(view.lifecycleProvider)
            .subscribe(object : CustomConsumer<Response<List<String>>>{
                override fun onSuccess(result: Response<List<String>>?) {
                    if (null != result) {
//                        L.d("retCode: " + result.retCode)
//                        L.d("list: " + result.data.toString())
                        if (null != result.data) {
                            view.mediaListSuccess(result.data)
                        } else {
                            view.mediaListError()
                        }
                    }
                }

            }, Consumer<Throwable> { e -> L.d(e.message) })
    }

    override fun cancel() {
        mMediaListDispose?.dispose()
    }

}

/**
 * 媒体组件
 */
@Component(dependencies = [ApplicationComponentImpl::class])
interface MediaComponent {
    /**
     * 注入媒体 Fragment
     */
    fun inject(fragment: MediaFragment)
}